package cj.web.admin.controllers;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.dao.LkgVideoDAO;
import cj.web.admin.domain.LkgVideo;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/video")
public class VideoController extends BaseController{
	
	private static Logger logger = Logger.getLogger(VideoController.class.getName());
	
	public final static String mp4=".mp4";
	
	@Autowired
	private LkgVideoDAO lkgVideoDao;

	@Get("show")
	public String show(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdscsp);
		return "/views/news/videoList.vm";
	}
	
	@Post("getList")
	public String getUserList(LkgVideo lkgVideo) {
		
		JSONObject jo = new JSONObject();
		
		List<LkgVideo> objs = lkgVideoDao.query(lkgVideo);
		for(LkgVideo obj:objs)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}
		
		int size = lkgVideoDao.querysize(lkgVideo);
		jo.put("data", objs);
		jo.put("draw", lkgVideo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	//新方法
	@Get("upload")
	public String uplaod(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdscsp);
		return "/views/news/videoDetail.vm";
	}
	
	//新方法
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile[] files,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
    	String path = ReadConfigProperties.getValue("uploadVideoPath");// 文件保存路径  
		
		// 判断文件列表是否为空  
        if (files != null && files.length > 0) {
        	for(MultipartFile file : files){
        		try {  
	            	//判断文件是否存在，不存在创建文件
	            	Map rep = ReturnUUID.getFolder(path);
	            	//所在的全路径
	            	path = rep.get("allpath").toString();
	            	//文件名
	                String fileName =  file.getOriginalFilename(); 
	                //统一转换为mp4
	                String newFileName = fileName.substring(0,fileName.lastIndexOf("."))+mp4;
	                //转存文件  
	                file.transferTo(new File(path+"/"+newFileName));  
	                //JSON 文件路径
	                jo.put("filePath", rep.get("filepath")+"/"+newFileName);
	                
	                LkgVideo lkgVideo = new LkgVideo();
	                lkgVideo.setName(rep.get("filepath")+"/"+newFileName);
	                lkgVideo.setPath(rep.get("filepath")+"/"+newFileName);
	                lkgVideo.setUpdateUser(returnSessionObj(request).getAccount());
	                lkgVideo.setCreateUser(returnSessionObj(request).getAccount());
	                lkgVideoDao.create(lkgVideo);
	                
                } catch (Exception e) {  
                    e.printStackTrace();  
                    jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
                }  
        	}
            
        } 
		
		return "@"+jo.toString();
    }
}
